package cate.game.activity.wishblessing;

import cate.common.table.activity.wishblessing.row.WishBlessingBanRow;
import cate.common.table.activity.wishblessing.row.WishBlessingBaseRow;
import cate.common.table.activity.wishblessing.row.WishBlessingEventRow;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.wishblessing.msg.WishBlessingDataResp;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class RoleWishBlessing extends RoleActivitySingle {
    @NoteField("选择的禁用英雄<阵营，<位置，英雄tid>>")
    public Map<Byte, Map<Byte, Integer>> selectMap;

    @NoteField("主动解锁的ban位数量<阵营，数量>")
    public Map<Byte, Integer> banMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (selectMap == null) {
            selectMap = new HashMap<>();
        }
        if (banMap == null) {
            banMap = new HashMap<>();
        }
    }

    @Override
    public void onRoleOpen() {
        List<WishBlessingBaseRow> rows = role.getGame().table.wishBlessing.base.getAllBaseRows(configTid());
        for (WishBlessingBaseRow row : rows) {
            if (row.freeBanNum > 0) {
                banMap.put(row.camp, row.freeBanNum);
            }
        }
    }

    @Override
    public void onRoleEnd() {
        selectMap.clear();
        banMap.clear();
    }

    @Override
    public void initSend() {
        role.sendNow(new WishBlessingDataResp(this));
    }

    @Override
    public void excelCheck() {

    }

    public WishBlessingBaseRow baseRow(byte camp) {
        return role.getGame().table.wishBlessing.base.getBaseRow(configTid(), camp);
    }

    public GameResult<Void> cancel(byte camp, byte pos) {
        GameResult<Void> r = new GameResult<>();
        WishBlessingBaseRow baseRow = baseRow(camp);
        if (baseRow == null) {
            return r.fail("不存在的阵营噢");
        }
        if (!banMap.containsKey(camp)) {
            return r.fail("您未解锁该阵营的禁用位置");
        }
        if (banMap.get(camp) < pos) {
            return r.fail("您未解锁该禁用位置噢");
        }
        Map<Byte, Integer> posHeros = selectMap.get(camp);
        if (posHeros == null) {
            return r.fail("您未选择过禁用英雄");
        }
        if (!posHeros.containsKey(pos)) {
            return r.fail("该位置无禁用英雄");
        }
        posHeros.remove(pos);
        initSend();
        return r;
    }

    public GameResult<Void> select(int tid, byte pos) {
        GameResult<Void> r = new GameResult<>();
        HeroBaseRow row = role.getGame().table.hero.base.get(tid);
        if (row == null) {
            return r.fail("英雄不存在");
        }
        WishBlessingBaseRow baseRow = baseRow(row.camp);
        if (baseRow == null) {
            return r.fail("无法禁用该英雄");
        }
        if (!banMap.containsKey(row.camp)) {
            return r.fail("您未解锁该禁用位置");
        }
        if (banMap.get(row.camp) < pos) {
            return r.fail("您未解锁该禁用位置噢");
        }
        if (!role.getGame().table.wishBlessing.ban.existHero(baseRow.group, tid)) {
            return r.fail("无法禁用该英雄噢");
        }
        Map<Byte,Integer> typeSelect = selectMap.computeIfAbsent(row.camp, e -> new HashMap<>());
        typeSelect.put(pos, tid);
        initSend();
        return r;
    }

    public String costItemStr() {
        return role.getGame().table.wishBlessing.event.getLuckyItemStr(configTid());
    }

    public MixRes calcActExtraReward(String fragmentStr) {
        WishBlessingEventRow eventRow = role.getGame().table.wishBlessing.event.randomEvent(configTid());
        if (eventRow != null) {
            if (StringUtils.isNotBlank(eventRow.extraRewardStr)) {
                return new MixRes(eventRow.extraRewardStr);
            }
            return new MixRes(fragmentStr).multiplyDouble(eventRow.scale);
        }
        return new MixRes();
    }

    public boolean isLuckyItem(String fragmentStr) {
        return role.getGame().table.wishBlessing.lucky.isLuckyItem(configTid(), fragmentStr);
    }

    public GameResult<Void> unlock(byte camp) {
        GameResult<Void> r = new GameResult<>();
        WishBlessingBaseRow baseRow = baseRow(camp);
        if (baseRow == null) {
            return r.fail("无法解锁该禁用位置");
        }
        if (banMap.containsKey(camp) && banMap.get(camp) >= (baseRow.banNum + baseRow.freeBanNum)) {
            return r.fail("您已经解锁所有的禁用位置拉");
        }
        GameResult<MixResItem> consumeRes = new MixRes(baseRow.banCostStr).consume(role);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            return r.fail(consumeRes);
        }
        banMap.put(camp, banMap.getOrDefault(camp, 0) + 1);
        initSend();
        return r;
    }

    public List<String> banRewardList() {
        List<String> list = new ArrayList<>();
        for (Map.Entry<Byte, Map<Byte, Integer>> entry : selectMap.entrySet()) {
            WishBlessingBaseRow baseRow = baseRow(entry.getKey());
            if (baseRow == null) {
                continue;
            }
            for (Integer heroId : entry.getValue().values()) {
                WishBlessingBanRow banRow = role.getGame().table.wishBlessing.ban.getHeroRow(baseRow.group, heroId);
                if (banRow != null) {
                    list.add(banRow.heroPid);
                }
            }
        }
        return list;
    }
}
